import '../../public.dart';

class CachedImage extends StatelessWidget {
  final String url;
  final BoxFit fit;
  CachedImage({@required this.url, this.fit = BoxFit.fill});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ExtendedImage.network(url, fit: fit, cache: true,
        loadStateChanged: (ExtendedImageState state) {
      switch (state.extendedImageLoadState) {
        case LoadState.loading:
          return Container(
            color: Colors.black12,
          );
          break;
        case LoadState.completed:
          return ExtendedRawImage(
            image: state.extendedImageInfo?.image,
            fit: fit,
          );
          break;
        case LoadState.failed:
          // TODO: Handle this case.
          return Image.asset(
            ImageHelper.wrapAssets("default_cover.jpg"),
            fit: fit,
          );
          break;
        default:
          return CircularProgressIndicator();
      }
    });
  }
}

class CatchImageHelper {
  Future<bool> clearAll() async {
    return await clearDiskCachedImages();
  }

  Future<bool> clearUrl(String url) async {
    return await clearDiskCachedImage(url);
  }

  void clearMemory() {
    return clearMemoryImageCache();
  }

  Future<bool> saveNetworkImageToPhoto(String url,
      {bool useCache: true}) async {
    var data = await getNetworkImageData(url, useCache: useCache);
    var filePath = await ImagePickerSaver.saveFile(fileData: data);
    return filePath != null && filePath != "";
  }
}

class PreviewImages extends StatefulWidget {
  final List<String> urls;
  final int currentIndex;
  PreviewImages({@required this.urls, this.currentIndex = 0});
  @override
  _PreviewImages createState() => _PreviewImages();
}

class _PreviewImages extends State<PreviewImages> {
  int currentIndex = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    FlutterStatusbarManager.setStyle(StatusBarStyle.LIGHT_CONTENT);
    FlutterStatusbarManager.setColor(Colors.black);
    currentIndex = widget.currentIndex;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      color: Colors.black,
      height: ScreenUtil().setHeight(1334),
      width: ScreenUtil().setWidth(750),
      child: ExtendedImageGesturePageView.builder(
        itemBuilder: (BuildContext context, int index) {
          var item = widget.urls[index];
          Widget image = ExtendedImage.network(
            item,
            fit: BoxFit.contain,
            mode: ExtendedImageMode.gesture,
            initGestureConfigHandler: (state) {
              return GestureConfig(
                minScale: 0.9,
                animationMinScale: 0.7,
                cacheGesture: false,
                maxScale: 3.0,
                animationMaxScale: 3.5,
                speed: 1.0,
                inertialSpeed: 100.0,
                initialScale: 1.0,
                inPageView: false,
                initialAlignment: InitialAlignment.center,
              );
            },
          );
          image = Container(
            child: image,
            padding: EdgeInsets.all(5.0),
          );
          if (index == currentIndex) {
            return Hero(
              tag: item + index.toString(),
              child: image,
            );
          } else {
            return image;
          }
        },
        itemCount: widget.urls.length,
        onPageChanged: (int index) {
          currentIndex = index;
//        rebuild.add(index);
        },
        controller: PageController(
          initialPage: currentIndex,
        ),
        scrollDirection: Axis.horizontal,
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    FlutterStatusbarManager.setStyle(StatusBarStyle.DEFAULT);
    FlutterStatusbarManager.setColor(Colors.transparent);
    super.dispose();
  }
}
